﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

// 角色管理器
public class RoleMgr : Singleton<RoleMgr>
{
    public int MainRoleThisId = -1; // 当前主角的thisid

    public MainRole MainRole;

    Dictionary<int, Role> _allRole = new Dictionary<int, Role>();

    // 响应主角thisid消息
    public static void OnMainRoleThisId(NCmd.Cmd cmd)
    {
        NCmd.MainRoleThisid thisidCmd = cmd as NCmd.MainRoleThisid;
        RoleMgr.Instance.MainRoleThisId = thisidCmd.ThisID;
    }

    // 响应场景中添加角色消息
    public static void OnAddSceneRole(NCmd.Cmd cmd)
    {
        NCmd.AddSceneRole roleCmd = cmd as NCmd.AddSceneRole;

        var roleId = roleCmd.ModelID;
        var tableData = RoleTable.Instance[roleId];
        var rolePath = tableData.ModelPath;

        var obj = ResMgr.Instance.GetGameObject(rolePath);

        Role role;
        if (RoleMgr.Instance.MainRoleThisId == roleCmd.ThisID)
        {
            // 是主角
            var mainRole = obj.AddComponent<MainRole>();
            role = mainRole;
            RoleMgr.Instance.MainRole = mainRole;
            CameraControl.Instance.m_followTarget = role.transform;
        }
        else
        {
            // 不是主角
            role = obj.AddComponent<Role>();
        }
        role.Init(roleCmd, tableData);
        RoleMgr.Instance._allRole.Add(roleCmd.ThisID, role);
    }

    public void Reset()
    {
        MainRole = null;
        // 清空角色列表
        _allRole.Clear();
    }
}
